<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Library extends Model
{
    protected $table = 'reds';
    protected $primaryKey = 'id';

    public $timestamps = false;

    protected $fillable=[];

    /**
     * 总金额排行榜
     * @return [type] [description]
     */
    public static function rankingList() {
        $resultList = [];
        //获取排行榜
        foreach (self::selectRaw("openid,sum(price) as total")->groupBy("openid")->orderBy("total","desc")
            ->take(20)->get() as $item) {
            $resultList[]  = $item->toArray();
        }
        $openIds = array_column($resultList,"openid");
        //获取会员信息
        $memberList = Member::listMember($openIds,"openid");
        //整合信息
        foreach ($resultList as &$item) {
            $item['total'] =  sprintf("%.2f",$item['total']);
            if (isset($memberList[$item['openid']])){
                $item['nickName'] = $memberList[$item['openid']]['username'];
                $item['avatarUrl'] = $memberList[$item['openid']]['head_ico'];
            }
        }
        return $resultList;
    }

}
